import SoftKey from './SoftKey';

export const mSoftKey = new SoftKey();

export function FindUsbKey() {
  const ErrGetVer = '获取锁的版本时错误，错误码是：';
  const ErrGetVerEx = '获取锁的扩展版本时错误，错误码是：';
  const ErrGetID = '获取锁的ID时错误，错误码是：';
  const MsgMoreUKey = '发现系统中有多把锁，请只插入要操作的锁。';
  const MsgNoUKey = '未能在系统中发现加密锁.';
  const MsgNoAppointUKey = '未找到指定的加密锁';
  const MsgYesUKey = '检查到指定的加密锁';

  let KeyPath = mSoftKey.FindPort(1);
  let lasterror = mSoftKey.GetLastError();
  if (lasterror == 0) {
    console.log(MsgMoreUKey);
    return false;
  }

  KeyPath = mSoftKey.FindPort(0);
  lasterror = mSoftKey.GetLastError();
  if (lasterror != 0) {
    // g_Event.sender.send('GetUSBKeyInfo', '', '', '');
    console.log(MsgNoUKey);
    return false;
  }
  {
    const Ver = mSoftKey.NT_GetIDVersion(KeyPath);
    lasterror = mSoftKey.GetLastError();
    if (lasterror != 0) {
      console.log(ErrGetVer + lasterror);
      return false;
    }
    const VerEx = mSoftKey.NT_GetIDVersionEx(KeyPath);
    lasterror = mSoftKey.GetLastError();
    if (lasterror != 0) {
      console.log(ErrGetVerEx);
      return false;
    }

    const IDInfo = mSoftKey.GetID(KeyPath);
    if (lasterror != 0) {
      console.log(ErrGetID);
      return false;
    }
    if (mSoftKey.CheckKeyByReadEprom() != 0) {
      console.log(MsgNoAppointUKey);
      return false;
    }
    if (mSoftKey.CheckKeyByFindort_2() != 0) {
      console.log(MsgNoAppointUKey);
      return false;
    }
    if (mSoftKey.CheckKeyByEncstring() != 0) {
      console.log(MsgNoAppointUKey);
      return false;
    }
    if (mSoftKey.CheckKeyByEncstring_New() != 0) {
      console.log(MsgNoAppointUKey);
      return false;
    }
    console.log(
      MsgYesUKey,
      IDInfo.ID_1 + IDInfo.ID_2,
      Ver!.toString(),
      VerEx!.toString(),
    );
    return true;
  }
}
